package com.asktgapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.asktgapp.R;

/**
 * time:2018/1/22
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class CustomIndicate extends View {
    private int count;
    //默认的每个圆点的大小
    private int defaultSize;
    private Paint normalPaint;
    private Paint selectPaint;
    private int mNormalColor;
    private int mSelectedColor;
    private int itemMargin;
    private int positon;


    public CustomIndicate(Context context, int count) {
        super(context);
        this.count = count;
        init(null);
    }

    public CustomIndicate(Context context, @Nullable AttributeSet attrs, int count) {
        super(context, attrs);
        this.count = count;
        init(attrs);
    }

    public CustomIndicate(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int count) {
        super(context, attrs, defStyleAttr);
        this.count = count;
        init(attrs);
    }

    void init(AttributeSet attrs) {
        final float dp = getResources().getDisplayMetrics().density;
        positon = 0;
        if (attrs != null) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CustomIndicate);
            mNormalColor = ta.getColor(R.styleable.CustomIndicate_normal_color, Color.WHITE);
            mSelectedColor = ta.getColor(R.styleable.CustomIndicate_selected_color, Color.YELLOW);
            itemMargin = ta.getInt(R.styleable.CustomIndicate_item_margin, (int) (dp * 10));
            ta.recycle();
        } else {
            mNormalColor = Color.WHITE;
            mSelectedColor = Color.YELLOW;
            itemMargin = (int) (dp * 10);
        }
        defaultSize = (int) (dp * 5);

        normalPaint = new Paint();
        normalPaint.setColor(mNormalColor);
        normalPaint.setStrokeWidth(2);
        normalPaint.setAntiAlias(true);
        normalPaint.setDither(true);
        normalPaint.setStyle(Paint.Style.FILL);


        selectPaint = new Paint();
        selectPaint.setColor(mSelectedColor);
        selectPaint.setAntiAlias(true);
        selectPaint.setStrokeWidth(2);
        normalPaint.setDither(true);
        selectPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int w = measureWidth(widthMeasureSpec);
        int h = measureHeight(heightMeasureSpec);
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
        //宽度没指定,但高度指定
        if (w == -1) {
            if (h != -1) {
                w = h * count + itemMargin * (count - 1);//宽度=高*数量
                defaultSize = h - 1;
            } else {//两个都不知道,默认宽高
                w = defaultSize * count + itemMargin * (count - 1);
                h = defaultSize;
            }
        } else {//宽度已知
            if (h == -1) {//高度不知道
                h = (w - itemMargin * (count - 1)) / count;
                defaultSize = h - 1;
            }
        }
        setMeasuredDimension(Math.min(w, wsize), Math.min(h, hsize));
    }


    private int measureWidth(int widthMeasureSpec) {
        //宽度
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        if (wmode == MeasureSpec.AT_MOST) {//wrap_content
            return -1;
        }
        return wsize;
    }

    private int measureHeight(int heightMeasureSpec) {
        //高度
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
        if (hmode == MeasureSpec.AT_MOST) {//wrap_content
            return -1;
        }
        return hsize;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < count; i++) {
            if (i == positon) {
                canvas.drawCircle(defaultSize / 2 + itemMargin * i, defaultSize / 2, defaultSize / 2 - 1, selectPaint);
            } else {
                canvas.drawCircle(defaultSize / 2 + itemMargin * i, defaultSize / 2, defaultSize / 2 - 1, normalPaint);
            }
        }
    }


    public void setPosition(int positon) {
        this.positon = positon;
        invalidate();
    }
}
